package org.cn.osiris.pet.controller;

import io.swagger.annotations.ApiOperation;
import org.cn.osiris.pet.domain.PetType;
import org.cn.osiris.pet.service.IPetService;
import org.cn.osiris.pet.domain.Pet;
import org.cn.osiris.pet.query.PetQuery;
import org.cn.osiris.basic.util.AjaxResult;
import org.cn.osiris.basic.util.PageList;
import org.cn.osiris.system.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    public IPetService petService;

    /**
     * 接口：添加或修改
     * @param pet  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    @PreAuthorize(name = "添加或修改",sn = "Pet:save")
    public AjaxResult addOrUpdate(@RequestBody Pet pet){
        try {
            if( pet.getId()!=null) {
                petService.update(pet);
            } else {
                petService.add(pet);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "删除",sn = "Pet:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            petService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    @PreAuthorize(name = "批量删除",sn = "Pet:patchDel")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
                petService.batchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    @PreAuthorize(name = "查找单个",sn = "Pet:getOne")
    public Pet findById(@PathVariable("id")Long id) {
        return petService.findById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    @PreAuthorize(name = "查找所有",sn = "Pet:getAll")
    public List<Pet> findAll(){
        return petService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @PreAuthorize(name = "分页",sn = "Pet:list")
    public PageList<Pet> queryPage(@RequestBody PetQuery query) {
        return petService.queryPage(query);
    }


    /**
     *
     * @param ids
     * @return 上架
     */
    @PatchMapping("/onsale")
//    @PreAuthorize(name = "上架",sn = "Pet:patchDel")
    public AjaxResult onsale(@RequestBody List<Long> ids){
        try {

            return petService.onsale(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    /**
     *
     * @param ids
     * @return 下架
     */
    @PatchMapping("/offsale")
//    @PreAuthorize(name = "上架",sn = "Pet:patchDel")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            return petService.offsale(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

}
